#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <shuju.h>
#include <QIcon>
#include <QTimer>
#include <map.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    shengli.load(":/pic/res/shengli.png");
    shibai.load(":/pic/res/shibai.png");
    intsence();
    PlayGame();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::intsence()
{
    //构建窗口以及图标
    setFixedSize(sence_w , sence_h);
    setWindowTitle("超级飞机大战2.0");
    setWindowIcon(QIcon(":/pic/res/boss.png"));
    //定时器设置
    Timer.setInterval(10);
    //初始化飞机出场间隔
    e_recorder = 0;
    //随机数字种子
    srand((unsigned int)time(NULL));

}

void MainWindow::PlayGame()
{
    //启动定时器
    Timer.start();
    //监听定时器
    connect(&Timer , &QTimer::timeout, [=](){
        //更新坐标
        updatePosition();
        //敌方小飞机飞机出航
        enemyout();
        update();
        //碰撞检测；
        pengzhuang();
        if(win||lost)
        {
            updatePosition();
            Timer.setInterval(10000000);
        }

    });
}

void MainWindow::updatePosition()
{
    //更新地图
    m_map.maplode();
    //射出子弹；
    myplane.shoot();
    for(int i = 0 ; i < 30 ; i++)
    {
        if(myplane.bullets[i].b_free == 0)
        {
            myplane.bullets[i].updatePosition();
        }
    }
    //敌机位置更新
    for(int m = 0 ; m < 30; m++)
    {
        if(enemys[m].e_free == 0)
        {
            enemys[m].updateposition();
        }
    }
    //boss位置更新
    bossmove();
    //boss射出子弹
    m_boss.shoot();
    for(int e = 0 ; e < 5 ; e++)
    {
        if(m_boss.bullets1[e].b_free==0)
        {
            m_boss.bullets1[e].updatePosition();
        }
    }
    for(int f = 0 ; f < 5 ; f++)
    {
        if(m_boss.bullets2[f].b_free == 0)
        {
            m_boss.bullets2[f].updatePosition();
        }
    }
    for(int f = 0 ; f < 5 ; f++)
    {
        if(m_boss.bullets3[f].b_free == 0)
        {
            m_boss.bullets3[f].updatePosition();
        }
    }
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(0 , m_map.map1_y , m_map.map1);
    painter.drawPixmap(0 , m_map.map2_y , m_map.map2);
    //绘制飞机
    painter.drawPixmap(myplane.x , myplane.y ,myplane.plane);
    //绘制boss；
    painter.drawPixmap(m_boss.x , m_boss.y ,m_boss.Boss);
    //绘制自己子弹
    for(int i = 0 ; i < 30 ; i++)
    {
        if(myplane.bullets[i].b_free ==0)
        {
            painter.drawPixmap(myplane.bullets[i].b_x ,myplane.bullets[i].b_y ,myplane.bullets[i].Bullet);
        }
    }
    //绘制boss子弹
    for(int i = 0 ; i < 5 ; i++)
    {
        if(m_boss.bullets1[i].b_free ==0)
        {
            painter.drawPixmap(m_boss.bullets1[i].b_x ,m_boss.bullets1[i].b_y ,m_boss.bullets1[i].Bullet);
        }
    }
    for(int n = 0 ; n < 5 ; n++)
    {
        if(m_boss.bullets2[n].b_free ==0)
        {
            painter.drawPixmap(m_boss.bullets2[n].b_x ,m_boss.bullets2[n].b_y ,m_boss.bullets2[n].Bullet);
        }
    }
    for(int n = 0 ; n < 5 ; n++)
    {
        if(m_boss.bullets3[n].b_free ==0)
        {
            painter.drawPixmap(m_boss.bullets3[n].b_x ,m_boss.bullets3[n].b_y ,m_boss.bullets3[n].Bullet);
        }
    }
    //绘制敌方小飞机
    for(int m = 0; m < 30; m++)
    {
        if(enemys[m].e_free == 0)
        {
            painter.drawPixmap(enemys[m].e_x , enemys[m].e_y , enemys[m].e_plane);
        }
    }
    //绘制游戏结束
    if(win == 1)
    {
        painter.drawPixmap(200 , 200 ,shengli);
    }
    if(lost == 1)
    {
        painter.drawPixmap(50 , 100 ,shibai);
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent * event)
{
    int a = event->x()- myplane.Rect.width() * 0.5;
    int b = event->y()- myplane.Rect.height() * 0.5;
    if(a <=0)
        a=0;
    if(b <= 0)
        b=0;
    if(a >= sence_w - myplane.Rect.width())
        a = sence_w - myplane.Rect.width();
    if(b >= sence_h - myplane.Rect.height())
        b = sence_h - myplane.Rect.height();
    myplane.setposition(a , b);
}

void MainWindow::enemyout()
{
    e_recorder++;
    if(e_recorder <100)
        return;
    //可以出场；
    e_recorder = 0;
    for(int i = 0; i < 30;i++)
    {
        if(enemys[i].e_free == 1)
        {
            enemys[i].e_free = 0;
            //敌机出坐标
           enemys[i].e_x = rand()%(sence_w - enemys[i].e_Rect.width());
            enemys[i].e_y =-enemys[i].e_plane.height() ;
            break;
        }
    }
}

void MainWindow::bossmove()
{
    if((m_boss.left==0)&&(m_boss.rigte==1))
    {
        if((m_boss.x+m_boss.b_Rect.width())>=sence_w)
        {
            m_boss.left=1;
            m_boss.rigte=0;
        }
        m_boss.x += 2;
    }
    if((m_boss.left==1)&&(m_boss.rigte==0))
    {
        if((m_boss.x)<= 0)
        {
            m_boss.left=0;
            m_boss.rigte=1;
        }
        m_boss.x -= 2;
    }
}
void MainWindow::pengzhuang()
{
    //敌机与我方子弹碰撞
    for(int i = 0; i < 30 ; i++)
    {
        if(enemys[i].e_free == 1)
            continue;
        for(int m = 0; m <30; m++)
        {
            if(myplane.bullets[m].b_free==1)
                continue;
            if(enemys[i].e_Rect.intersects(myplane.bullets[m].b_Rect))
            {
                myplane.bullets[m].b_free = 1;
                enemys[i].blood --;
                if(enemys[i].blood==0)
                {
                    enemys[i].e_free = 1;
                    enemys[i].blood = 2;
                }
            }
        }
    }
    //我与敌方敌机碰撞
    for(int z =0 ; z < 30 ; z++)
    {
        if(enemys[z].e_free == 1)
            continue;
        if(enemys[z].e_Rect.intersects(myplane.Rect))
        {
            lost = 1;
        }
    }
    //我与敌方boss碰撞；
    if(m_boss.b_Rect.intersects(myplane.Rect))
    {
        lost = 1;
    }
    //我方子弹与敌方boss碰撞

    for(int i =0 ; i<30 ; i++)
    {
        if(m_boss.b_Rect.intersects(myplane.bullets[i].b_Rect))
        {
            myplane.bullets[i].b_free =1;
            m_boss.blood --;
            if(m_boss.blood ==0)
            {
                win = 1;
            }
        }
    }
    //boss子弹于我的碰撞
    for(int i = 0 ; i < 5 ; i++)
    {
        if(m_boss.bullets1[i].b_Rect.intersects(myplane.Rect))
        {
            lost = 1;
        }
    }
    for(int i = 0 ; i < 5 ; i++)
    {
        if(m_boss.bullets2[i].b_Rect.intersects(myplane.Rect))
        {
            lost = 1;
        }
    }
    for(int i = 0 ; i < 5 ; i++)
    {
        if(m_boss.bullets3[i].b_Rect.intersects(myplane.Rect))
        {
            lost = 1;
        }
    }
}